/**
 * 
 */
package Model;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



/**
 * @author enzo
 *
 */
public class contact {

	private String name;
	private String firstname;
	private String address;
	private String companyAddress;
	private String Phoneperso; 
	private String Phonepro; 
	private String URL;
	private String emailPerso;
	private String emailPro;
	private String photo;
	private String group;
	
	public contact()
	{ 
		name=new String();
		firstname=new String();
		address=new String();
		companyAddress=new String();
		Phoneperso=new String();
		Phonepro=new String();
		URL=new String();
		emailPerso=new String();
		emailPro=new String();
		photo=new String();
	}
	public contact (String n,String f, String a,String c,String phoneperso,String phonepro,String eperso,String epro,String u,String p,String g)
	{
		
		if(!n.isEmpty())name=n;
		else name=" ";
		if(!f.isEmpty())firstname=f;
		else firstname=" ";
		if(!a.isEmpty())address=a;
		else address=" ";
		if(!c.isEmpty())companyAddress=c;
		else companyAddress=" ";
		if(!phoneperso.isEmpty())this.Phoneperso=phoneperso;
		else Phoneperso=" ";
		if(!phonepro.isEmpty())this.Phonepro=phonepro;
		else Phonepro=" ";
		if(!u.isEmpty())URL=u;
		else URL=" ";
		if(!eperso.isEmpty())emailPerso=eperso;
		else emailPerso=" ";
		if(!epro.isEmpty())emailPro=epro;
		else emailPro=" ";
		if(p.isEmpty())
		{
			photo="Resources/Mysterion.png";
			System.out.println("ttotot");
		}
		else {
			photo=p;
			if(!photo.contentEquals("Resources/Mysterion.png") && !photo.contentEquals("Resources/"+this.firstname+this.name+".png")) savePicture();
		}
		if(!g.isEmpty())group=g;
		else group=" ";
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		if(!name.isEmpty()) this.name = name;
		else this.name=" ";
	}
	/**
	 * @return the firstname
	 */
	public String getFirstname() {
		return firstname;
	}
	/**
	 * @param firstname the firstname to set
	 */
	public void setFirstname(String firstname) {
		if(!name.isEmpty()) this.firstname = firstname;
		else this.firstname=" ";
	}
	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		if(!address.isEmpty())this.address = address;
		else this.address=" ";
	}
	/**
	 * @return the companyAddress
	 */
	public String getCompanyAddress() {
		return companyAddress;
	}
	/**
	 * @param companyAddress the companyAddress to set
	 */
	public void setCompanyAddress(String companyAddress) {
		if(!companyAddress.isEmpty())this.companyAddress = companyAddress;
		else this.companyAddress=" ";
	}
	/**
	 * @return the phoneperso
	 */
	public String getPhoneperso() {
		return Phoneperso;
	}
	/**
	 * @param phoneperso the phoneperso to set
	 */
	public void setPhoneperso(String phoneperso) {
		if(!phoneperso.isEmpty())Phoneperso = phoneperso;
		else Phoneperso=" ";
	}
	/**
	 * @return the phonepro
	 */
	public String getPhonepro() {
		return Phonepro;
	}
	/**
	 * @param phonepro the phonepro to set
	 */
	public void setPhonepro(String phonepro) {
		if(!phonepro.isEmpty())Phonepro = phonepro;
		else Phonepro=" ";
	}
	/**
	 * @return the uRL
	 */
	public String getURL() {
		return URL;
	}
	/**
	 * @param uRL the uRL to set
	 */
	public void setURL(String uRL) {
		if(!uRL.isEmpty())URL = uRL;
		else URL=" ";
	}
	/**
	 * @return the emailPerso
	 */
	public String getEmailPerso() {
		return emailPerso;
	}
	/**
	 * @param emailPerso the emailPerso to set
	 */
	public void setEmailPerso(String emailPerso) {
		if(!emailPerso.isEmpty())this.emailPerso = emailPerso;
		else this.emailPerso=" ";
	}
	/**
	 * @return the emailPro
	 */
	public String getEmailPro() {
		return emailPro;
	}
	/**
	 * @param emailPro the emailPro to set
	 */
	public void setEmailPro(String emailPro) {
		if(!emailPro.isEmpty())this.emailPro = emailPro;
		else this.emailPro=" ";
	}
	/**
	 * @return the photo
	 */
	public String getPhoto() {
		return photo;
	}
	/**
	 * @param photo the photo to set
	 */
	public void setPhoto(String photo) {
		if(!photo.isEmpty())
			{
			this.photo = photo;
			savePicture();
			}
		else this.photo=" ";
	}
	/**
	 * @return the group
	 */
	public String getGroup() {
		return group;
	}
	/**
	 * @param group the group to set
	 */
	public void setGroup(String group) {
		if(!group.isEmpty()) this.group = group;
		else this.group=" ";
	}
	public void savePicture()
	{
		
		OutputStream out;
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(this.photo));
			out = new BufferedOutputStream(new FileOutputStream("Resources/"+this.firstname+this.name+".png"));
			byte[] buf = new byte[4096];
			int n;
			while ((n=in.read(buf, 0, buf.length)) > 0)
			out.write(buf, 0, n);
			 
			in.close();
			out.close();
			this.photo="Resources/"+this.firstname+this.name+".png";
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String toCsv()
	{
		StringBuilder sb=new StringBuilder();
		sb.append(name).append(";").append(firstname).append(";").append(address).append(";").append(companyAddress).append(";").append(Phoneperso).append(";").append(Phonepro).append(";").append(emailPerso).append(";").append(emailPro).append(";").append(URL).append(";").append(photo).append(";").append(group).append("\n");
		
		//String s = new String(name+";"+firstname+";"+address+";"+companyAddress+";"+Phoneperso+";"+Phonepro+";"+emailPerso+";"+emailPro+";"+URL+";"+photo+";"+group+"\n");
		return sb.toString();
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((Phoneperso == null) ? 0 : Phoneperso.hashCode());
		result = prime * result
				+ ((emailPerso == null) ? 0 : emailPerso.hashCode());
		result = prime * result
				+ ((firstname == null) ? 0 : firstname.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		contact other = (contact) obj;
		if (Phoneperso == null) {
			if (other.Phoneperso != null)
				return false;
		} else if (!Phoneperso.equals(other.Phoneperso))
			return false;
		if (emailPerso == null) {
			if (other.emailPerso != null)
				return false;
		} else if (!emailPerso.equals(other.emailPerso))
			return false;
		if (firstname == null) {
			if (other.firstname != null)
				return false;
		} else if (!firstname.equals(other.firstname))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
